/* $Id: DJudgeServlet.java 133 2011-08-26 12:01:00Z altdotua@gmail.com $ */

package alt.djudge.frontend.server.servlets;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import alt.djudge.frontend.server.HibernateUtil;
import alt.djudge.frontend.server.datatypes.ContestProblemEntry;
import alt.djudge.frontend.server.datatypes.SubmissionEntry;
import alt.djudge.frontend.server.datatypes.UserEntry;
import alt.djudge.frontend.server.models.ContestProblemsModel;
import alt.djudge.frontend.server.models.SubmissionsModel;
import alt.djudge.frontend.server.models.UsersModel;
import alt.djudge.frontend.server.models.scores.RejudgeModel;
import alt.djudge.frontend.server.utils.Linker;

public class DJudgeServlet extends AbstractServlet
{
	private static final long serialVersionUID = 1L;

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
	{
		req.setCharacterEncoding("UTF-8");
		
		String action = req.getParameter("action");
		if ("get".equals(action))
		{
			resp.setCharacterEncoding("utf8");
			resp.setContentType("text/xml");
			DJudgeUtils.printDJudgeXml(resp.getOutputStream());
		}
		else if ("post".equals(action))
		{
			DJudgeUtils.putXml(req.getParameter("id"), req.getParameter("xml"));
		}
		else if ("executeSql".equals(action))
		{
			String sql = req.getParameter("sql");
			resp.getWriter().println(sql);
			HibernateUtil.executeSql(sql);
		}
		else if ("cache".equals(action))
		{
			//CacheModel.getStaticCache().clear();
			//resp.sendRedirect(Linker.link("stats.jsp"));
		}
		else if ("rejudgevirtualhard".equals(action))
		{
			RejudgeModel.rejudgeAllHardVirtual();
			resp.sendRedirect(Linker.link("stats.jsp"));
		}
		else if ("rejudgerealhard".equals(action))
		{
			RejudgeModel.rejudgeAllHardReal();
			resp.sendRedirect(Linker.link("stats.jsp"));
		}
		else if ("deletestats".equals(action))
		{
			RejudgeModel.deleteStats();
			resp.sendRedirect(Linker.link("stats.jsp"));
		}
		else if ("dupm".equals(action))
		{
			/*RejudgeModel.deleteStats();
			resp.sendRedirect(Linker.link("stats.jsp"));*/
			//files
			/*
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("contestId", 5);
			List<SubmissionEntry> submissionEntries = SubmissionsModel.getSubmissions(map);
			Map<Long, UserEntry> usersMap = UsersModel.getEntriesMap();
			Map<Long, ContestProblemEntry> problemsMap = ContestProblemsModel.getEntriesMap();
			for (SubmissionEntry s : submissionEntries)
			{
				//UserEntry user = usersMap.get(s.getUserId());
				//ContestProblemEntry problem = problemsMap.get(s.getContestProblemId());
				//File f = new File("files/" + (user.getUsername()) + "_" + problem.getSid() + "_" + s.getId() + ".txt");
				//new PrintWriter(f).println(SubmissionsModel.getSubmissionDetails(s.getId().toString()).getSourceCode());
			}
			*/
		}
	}
	
	public static void main(String[] args)
	{
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("contestId", 5L);
		List<SubmissionEntry> submissionEntries = SubmissionsModel.getSubmissions(map);
		Map<Long, UserEntry> usersMap = UsersModel.getEntriesMap();
		Map<Long, ContestProblemEntry> problemsMap = ContestProblemsModel.getEntriesMap();
		for (SubmissionEntry s : submissionEntries)
		{
			UserEntry user = usersMap.get(s.getUserId());
			ContestProblemEntry problem = problemsMap.get(s.getContestProblemId());
			File f = new File("files/" + (user != null ? user.getUsername() : "_problem_") + "_" + (problem != null ? problem.getName() : "__") + "_id_" + s.getId() + ".txt");
			f.getParentFile().mkdirs();
			System.out.println(f.getAbsolutePath());
			try
			{
				String src = SubmissionsModel.getSubmissionDetails(s.getId()).getSourceCode();
				System.out.println(src);
				PrintWriter pw = new PrintWriter(f);
				pw.write(src);
				pw.close();
			}
			catch (Exception e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}
